package com.iot08.api.service;

import com.iot08.common.entity.app.BloodOxygenRecord;
import com.iot08.common.entity.app.BloodPressureRecord;
import com.iot08.common.service.base.BaseService;
import com.iot08.common.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @version 1.0
 * @Description:
 * @author: Rock
 * @time: 2021/3/8 18:33
 */
@Slf4j
@Service
public class BloodOxygenRecordService extends BaseService<BloodOxygenRecord> {

    /**
     * @param imei
     * @param value
     * @time: 2021/3/9 16:39
     * @author: Rock
     * @version 1.0
     */
    public BloodOxygenRecord add(String imei, String value) {
        BloodOxygenRecord record = new BloodOxygenRecord();
        record.setImei(imei);
        record.setValue(value);
        record.setDateTime(System.currentTimeMillis() / 1000);
        record.setUpDayStr(DateUtil.dateToStr(new Date(), DateUtil.FORMAT_4));
        record.setExpiryAt(DateUtil.getSystemExpiryAt());
        return this.save(record);
    }

    /**
     * @param imei
     * @param upDayStr
     * @time: 2021/3/8 19:27
     * @author: Rock
     * @version 1.0
     */
    public List<Object> listData(String imei, String upDayStr) {
        List<Object> list = new ArrayList<>();
        Query query = new Query(Criteria.where("i").is(imei).and("upDayStr").is(upDayStr));
        List<BloodOxygenRecord> records = this.find(query.with(Sort.by(Sort.Order.desc("dateTime"))));
        for (BloodOxygenRecord bloodOxygenRecord : records) {
            Map<String, Object> temp = new HashMap<>();
            temp.put("time", bloodOxygenRecord.getDateTime());
            temp.put("value", bloodOxygenRecord.getValue());
            list.add(temp);
        }
        return list;
    }

}
